package cn.kgc.article.controller;

import cn.kgc.article.service.ArticleService;
import cn.kgc.common.entity.Article;
import cn.kgc.common.entity.DataPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by teacher ZHANG on 2023/1/31
 */
@Api(tags = "新闻内容管理")
@RestController
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @ApiOperation("列表显示新闻摘要")
    @ApiImplicitParams({
        @ApiImplicitParam(name="categoryId", value = "文章分类id", required = true, paramType = "query", dataType = "Integer"),
        @ApiImplicitParam(name="pageNum", value = "文章分页页码", required = true, paramType = "query", dataType = "Integer"),
        @ApiImplicitParam(name="pageSize", value = "文章分页大小", required = true, paramType = "query", dataType = "Integer")
    })
    @GetMapping("/articles")
    public DataPage<Article> listArticles(Integer categoryId, Integer pageNum, Integer pageSize) {
        //查询条件包装类
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.select("articleId", "title", "summary", "image", "createDate", "categoryId");
        if (categoryId != null && categoryId > 0) {
            wrapper.eq("categoryId", categoryId);
        }

        //m-plus分页
        IPage<Article> pageInfo = new Page<>(pageNum, pageSize);
        articleService.page(pageInfo, wrapper);

        DataPage<Article> dataPage = new DataPage<>();
        dataPage.setDataList(pageInfo.getRecords());
        dataPage.setRowCount((int)pageInfo.getTotal());
        dataPage.setPageCount((int)pageInfo.getPages());
        dataPage.setPageNum(pageNum);
        dataPage.setPageSize(pageSize);

        return dataPage;
    }

    //根据id查询
    @ApiOperation("根据ID显示新闻摘要或详情")
    @ApiImplicitParam(name="articleIds", value = "文章的ID，多个使用逗号分隔", required = true, paramType = "path", dataType = "String")
    @GetMapping("/articles/{articleIds}")
    public List<Article> findArticle(@PathVariable("articleIds") String articleIds) {
        String[] items = articleIds.split(",");

        //查询单个
        if (items.length == 1) {
            Article article = articleService.getById(Integer.parseInt(items[0]));
            return Arrays.asList(article);
        }

        List<Integer> list = new ArrayList<>();
        for (String item : items) {
            list.add(Integer.parseInt(item));
        }

        return articleService.listByArticleIds(list);
    }

    @PostMapping("/articles")
    @ApiOperation("新增文章内容")
    @ApiImplicitParam(name="article", value = "文章详情", required = true, paramType = "body", dataType = "Article")
    public Boolean saveArticle(@RequestBody Article article) {
        return articleService.saveArticle(article) > 0;
    }

    @PutMapping("/articles/{articleId}")
    @ApiOperation("修改文章内容")
    @ApiImplicitParams({
        @ApiImplicitParam(name="articleId", value = "文章的ID", required = true, paramType = "path", dataType = "Integer"),
        @ApiImplicitParam(name="article", value = "文章详情", required = true, paramType = "body", dataType = "Article")
    })
    public Boolean updateArticle(@PathVariable("articleId") Integer articleId, @RequestBody Article article) {
        return articleService.updateArticle(article) > 0;
    }

    @DeleteMapping("/articles/{articleId}")
    @ApiOperation("删除文章内容")
    @ApiImplicitParam(name="articleId", value = "文章的ID", required = true, paramType = "path", dataType = "Integer")
    public Boolean removeArticle(@PathVariable("articleId") Integer articleId) {
        return articleService.removeById(articleId);
    }
}
